﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class AdminControl_ImportNewProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ShoppingDataContext spdt = new ShoppingDataContext();
        var usefors = from p in spdt.USEFORs
                     select p;
        foreach (var uf in usefors)
        {
            this.usefor.Items.Add(uf.usefor1);
        }

        var types = from p in spdt.TYPEs
                      select p;
        foreach (var tp in types)
        {
            this.type.Items.Add(tp.type1);
        }

        var genders = from p in spdt.GENDERs
                    select p;
        foreach (var gd in genders)
        {
            this.gender.Items.Add(gd.gender1);
        }
    }
    protected void Continue_Click(object sender, EventArgs e)
    {
        ShoppingDataContext spdt = new ShoppingDataContext();
        var products = from p in spdt.PRODUCTs
                     select p;
        int temp = 1;
        int flag1 = 0;
        int flag = 1;
        int ProId = 0;
        string tam = "P";
        while (flag != 0)
        {
            foreach (var pro in products)
            {
                string id = pro.ProductID.Substring(1, 4);
                ProId = int.Parse(id);
                if (ProId == temp)
                {
                    temp++;
                    flag1 = 0;
                    break;
                }

                flag1 = 1;
            }
            if (flag1 == 1)
            {
                flag = 0;
                ProId = temp;
            }

        }
        for (int i = 1; i <= 4 - ProId.ToString().Length; i++)
        {
            tam = tam + "0";
        }
        tam = tam + ProId.ToString();

        string strFileNameWithPath = imageupload.PostedFile.FileName;
        string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPath);
        string strFileName = System.IO.Path.GetFileName(strFileNameWithPath);

        strExtensionName = strExtensionName.ToLower();
        if (strExtensionName.Equals(".jpg") || strExtensionName.Equals(".gif") || strExtensionName.Equals(".png"))
        {
            imageupload.PostedFile.SaveAs(Server.MapPath("~/Images/ImagesForProduct/") + strFileName);
        }

        PRODUCT product = new PRODUCT();
        product.ProductID = tam;
        product.ProName = this.proname.Text;
        product.usefor = this.usefor.SelectedValue;
        product.type = this.type.SelectedValue;
        product.gender = this.gender.SelectedValue;
        product.image = strFileName.Insert(0, "~/Images/ImagesForProduct/");
        product.Descript = this.descript.Text;
        product.countview = 0;
        spdt.PRODUCTs.InsertOnSubmit(product);
        spdt.SubmitChanges();
        Response.Redirect("../AdminTool/ImportProduct.aspx?proid="+tam);
    }
}
